<template>
    <div class="index">
        <div class="firstShow">
            
        </div>
        <div class="login">
            <form class="sign-in-form">
                <div class="tagChange">
                    <div class="tagText" @click="(e) => { toggle(e) }">
                        <div>短信登录</div>
                        <div>密码登录</div>
                    </div>
                    <div class="formText" v-if="isMobile">
                        <div class="input-field">
                            <i class="fas fa fa-mobile"></i>
                            <input type="text" class="mobile" v-model="form.phone" placeholder="请输入手机号" />
                        </div>
                        <div class="input-field">
                            <i class="fas fa fa-file"></i>
                            <input type="text" v-model="setPhoneCode" placeholder="请输入验证码" />
                            <a class="getcode" @click="getPhoneCode($event)">获取验证码</a>
                        </div>
                        <div class="input-field2">
                            <a href="">没有账号？</a>
                        </div>
                        <el-button class="loginbutton" @click="onSubmitLogin(1)">登录</el-button>
                    </div>
                    <div class="formText" v-if="!isMobile">
                        <div class="input-field">
                            <i class="fas fa-user"></i>
                            <input type="text" v-model="form.username" placeholder="请输入用户名" />
                        </div>
                        <div class="input-field">
                            <i class="fas fa-lock"></i>
                            <input type="password" v-model="form.password" placeholder="请输入密码" />
                        </div>
                        <el-button class="loginbutton" @click="onSubmitLogin(2)">登录</el-button>
                    </div>
                </div>
            </form>
        </div>
        <!-- 文字验证 -->
        <div class="verify" v-if="isVerify === true">
            <verify @isOkShow="okOk" :infos="infos" />
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import verify from './verify.vue' // 文字验证
import axios from 'axios'
import { watch } from "vue";
import { nextTick } from "vue";
const isVerify = ref(false) // 是否开启文字验证
const router = useRouter();
let infos = reactive({ token: '', info: {} })
const form = ref({
    phone: "",
    code: "",
    username: "admin",
    password: "admin",
});
// 是否是手机验证码登录
const isMobile = ref(true);
// 输入的验证码
let setPhoneCode = ref();
const okOk = (status) => { // 子串父
    console.log(status, "123")
}
watch(isVerify, (newValue, oldValue) => {
    // if(newValue === true){
    //     document.querySelector('.index').style.backgroundColor = "rgba(0, 0, 0, 0.5)"
    // }
})
const onSubmitLogin = (num) => {
    if (num === 1) { // 手机号登录

        if (!form.value.phone) {
            return ElMessage({ type: "error", message: "手机号不能为空！" });
        }
        if (!setPhoneCode.value) {
            return ElMessage({ type: "error", message: "验证码不能为空！" });
        }
        form.value.code = setPhoneCode.value;

        axios.post('http://localhost:3000/thy/codeLogin', form.value).then((res) => {
            if (res.data.code === 200) {
                infos.info = res.data.data
                infos.token = res.data.token
                nextTick(() => {
                    isVerify.value = true
                })
                // sessionStorage.setItem('token', JSON.stringify(res.data.token))
                // sessionStorage.setItem('userInfo', JSON.stringify(res.data.data))

                // router.push("/home")
            } else {
                ElMessage({ type: "error", message: "验证码不匹配" });
            }
        })
    } else { // 账号密码登录
        if (!form.value.username)
            return ElMessage({ type: "error", message: "用户名不能为空!" });
        if (!form.value.password)
            return ElMessage({ type: "error", message: "密码不能为空!" });

        axios.post('http://localhost:3000/thy/passLogin', form.value).then((res) => {
            if (res.data.code === 200) {
                infos.info = res.data.data
                infos.token = res.data.token
                nextTick(() => {
                    isVerify.value = true
                })
                // sessionStorage.setItem('userInfo', JSON.stringify(res.data.data))
                // sessionStorage.setItem('token', JSON.stringify(res.data.token))
                // isVerify.value = true
            } else if (res.data.code === 404) {
                ElMessage({ type: "error", message: "用户名与密码不匹配，请重新输入!" });
            } else {
                ElMessage({ type: "error", message: "用户名不存在，请重新输入" });
            }
        })
    }
};
const toggle = (e) => {
    let dom = document.querySelectorAll('.tagText div')
    dom.forEach(item => {
        item.style.borderBottom = "3px solid #e8ebef"
    })
    if (e.target.innerHTML === "短信登录") {
        isMobile.value = true
    } else {
        isMobile.value = false
    }
    e.target.style.borderBottom = "3px solid #00479d"
}

let timer = ref(60); // 倒计时
const getPhoneCode = async (event) => {
    if (!form.value.phone) {
        return ElMessage({ type: "error", message: "手机号不能为空!" });
    }
    // 验证格式
    let isphone = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
    if (!isphone.test(form.value.phone)) {
        return ElMessage({ type: "error", message: "手机号格式不正确!" });
    }
    if (timer.value != 60) return;

    setMobileCode(event);
};
// 发送验证码
const setMobileCode = async (event) => {
    let { data } = await axios.post("http://localhost:3000/thy/sendCode", {
        phone: form.value.phone,
    });

    if (data.code == 404) {
        return ElMessage({ type: "error", message: "发送失败" });
    }

    ElMessage({ type: "success", message: "发送成功！" });
    let a = undefined;
    a = setInterval(() => {
        if (timer.value <= 0) {
            clearInterval(a);
            timer.value = 60;
            event.target.innerText = "获取验证码";
            setPhoneCode.value = "";
            return;
        }
        timer.value--;
        event.target.innerText = "还剩" + timer.value + "秒";
    }, 1000);
};
</script>

<style lang="scss" scoped>
.index {
    width: 100%;
    height: 100%;
    background-image: url("https://bpic.588ku.com/back_pic/03/99/65/1357fa0dc415013.jpg");
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;

    position: relative;
    display: flex;
}

.firstShow {
    width: 50%;
    position: relative;
}

.login {
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
    left: 75%;
    width: 50%;
    transition: 1s 0.7s ease-in-out;
    display: grid;
    grid-template-columns: 1fr;
    z-index: 5;
}

// 小车平滑
@keyframes imgOneimg {
    0% {
        opacity: 0;
        transform: translateX(-500px)
    }

    20% {
        transform: translateX(-500px)
    }

    50% {
        transform: translateX(10px);
    }

    70% {
        transform: translateX(-20px);
    }

    100% {
        opacity: 1;
    }
}

.imgOne {
    width: 170px;
    margin: auto;
    position: absolute;
    top: 120px;
    bottom: 0;
    left: 50px;
    right: 0;
    animation: imgOneimg 3s forwards;
}

// 放大
@keyframes imgTwoimg {
    0% {
        opacity: 0;
        transform: scale(-0.5)
    }

    50% {
        transform: scale(1.2)
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.imgTwo {
    width: 580px;
    margin: auto;
    position: absolute;
    top: 0;
    bottom: 60px;
    left: 0px;
    right: 0;
    animation: imgTwoimg 4s forwards;
}

// 抖动
@keyframes imgThreeimg {
    0% {
        opacity: 0;
        // transform: translate(0px,-20px);
    }

    40% {
        transform: translate(0px, -20px);
    }

    50% {
        transform: translate(0px, 20px);
    }

    60% {
        transform: translate(0px, -50px);
    }

    100% {
        opacity: 1;
    }
}

.imgThree {
    width: 455px;
    margin: auto;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 10px;
    animation: imgThreeimg 1s forwards;
}

form {
    margin: auto;
    width: 40%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 0rem 5rem;
    transition: all 0.2s 0.7s;
    overflow: hidden;
    grid-column: 1 / 2;
    grid-row: 1 / 2;
    background-color: #fff;
    min-height: 500px;
    border-radius: 20px;
}

.tagChange {
    width: 100%;
    margin: auto;
}

.tagText {
    display: flex;
}

.tagText div {
    width: 50%;
    min-height: 30px;
    text-align: center;
    border-bottom: 3px solid #e8ebef;
    color: #2f69ae;
    cursor: pointer;
}

.tagText div:nth-child(1) {
    border-bottom: 3px solid #00479d;
}

.formText {
    text-align: center;
    margin-top: 40px;
}

.input-field {
    max-width: 380px;
    width: 97%;
    background-color: #f0f0f0;
    margin: 10px 0;
    height: 53px;
    border-radius: 10px;
    display: grid;
    grid-template-columns: 15% 55% 15%;
    padding: 0 .4rem;
    position: relative;
}
.input-field2{
    margin-left: 61%;
}
.input-field {
    text-align: center;
    line-height: 53px;
    color: #acacac;
    transition: 0.5s;
    font-size: .9rem;
    margin-top: 20px;
}

.input-field input {
    background: none;
    outline: none;
    border: none;
    line-height: 1;
    font-weight: 600;
    font-size: 1rem;
    color: #333;
}

.input-field input::placeholder {
    color: #aaa;
    font-weight: 500;
}

.getcode {
    width: 100px;
    height: 100%;
    position: relative;
    left: -16px;
}

.getcode:hover {
    background-color: transparent;
    cursor: pointer;
}

.verify {
    z-index: 5;
}

.verify::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #3c3b3b;
    opacity: 0.5;
    /* 半透明黑色 */
    z-index: -1;
    /* 将遮罩层的层级放到最底层 */
}

.loginbutton {
    margin-top: 30px;
    min-height: 45px;
    width: 100%;
    border-radius: 12px;
    background-color: #00479d;
    color: #fff;
}

// #00479d
</style>